<template>
  <el-dialog :title="title" :close-on-click-modal="false" v-model="open" width="70%" append-to-body>
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="设备类型名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备类型名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableData"
      ref="tableRef"
      border
      maxHeight="500px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备类型编号" align="center" prop="code" />
      <el-table-column
        label="设备类型名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="MatDialog">
import { listType } from "@/api/base/equipment/equipmentTypeApi";
const open = defineModel("show");
const currentData = defineModel();
const loading = ref(true);
const showSearch = ref(true);
const tableData = ref([]);
const tableRef = ref("tableRef");
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const selectData = ref([]);
const total = ref(0);
const title = ref("");
const queryRef = ref("queryRef");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipNo: undefined,
    name: undefined,
    status: undefined,
  },
});

const { queryParams } = toRefs(data);

const emit = defineEmits(["change"]);

/** 查询字典类型列表 */
function getList() {
  loading.value = true;
  listType(queryParams.value).then((response) => {
    tableData.value = response.rows;
    total.value = response.total;
    loading.value = false;

    setTableSelect();
  });
}

function setTableSelect() {
  const currentIds = currentData.value.map((el) => el.code);
  tableData.value.forEach((el) => {
    if (currentIds.includes(el.code))
      tableRef.value.toggleRowSelection(el, true);
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  queryRef.value.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  selectData.value = selection;
}

function submitForm() {
  open.value = false;
  emit("change", selectData.value);
}

getList();
</script>

<style lang="scss" scoped></style>
